package Application;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import DbConnection.database;


public class Category 
{
  public void addCategory(int parent_id, String catName)
  {
	  String query = "insert into Category (category_name, parent_id) values('" + catName + "','" + parent_id + "')";
	  try
	  {
		  database db = new database();
		  Connection conn = db.getConn();
		  Statement insert = conn.createStatement();
		  insert.executeUpdate(query);
	  }
	  catch(SQLException e) 
	  {
		  System.err.println("Mysql Statement Error: " + query);
		  e.printStackTrace();
	  }
  }

  public String getName(int id)
  {
	  String query = "select category_name from Category where category_id='" + id + "'";
	  try
	  {
		  database db = new database();
		  Connection conn = db.getConn();
		  Statement select = conn.createStatement();
	      ResultSet result = select.executeQuery(query);
	      result.first();	      
	      return result.getString(1);
	  }
	  catch(SQLException e) 
	  {
		  System.err.println("Mysql Statement Error: " + query);
		  e.printStackTrace();
	  }	  
	  return "error";
  }
  
  public void updateCategory(int id, String catName)
  {
	  String query = "update Category set category_name = '" + catName + "' where category_id= '" + id + "'";
	  try
	  {
		  database db = new database();
		  Connection conn = db.getConn();
		  Statement select = conn.createStatement();
	      select.executeUpdate(query);
	  }
	  catch(SQLException e) 
	  {
		  System.err.println("Mysql Statement Error: " + query);
		  e.printStackTrace();
	  }	    
  }
  
  public int getParentId(int id)
  {
	  String query = "select parent_id from Category where category_id='" + id + "'";
	  try
	  {
		  database db = new database();
		  Connection conn = db.getConn();
		  Statement select = conn.createStatement();
	      ResultSet result = select.executeQuery(query);
	      result.first();	      
	      return result.getInt(1);
	  }
	  catch(SQLException e) 
	  {
		  System.err.println("Mysql Statement Error: " + query);
		  e.printStackTrace();
	  }	  
	  return -1;
  }

  public String getParentName(int id)
  {
	  String query = "select category_name from category where category_id = (select parent_id from category where category_id = '" +  id + "')";
	  try
	  {
		  database db = new database();
		  Connection conn = db.getConn();
		  Statement select = conn.createStatement();
	      ResultSet result = select.executeQuery(query);
	      result.first();	      
	      return result.getString(1);
	  }
	  catch(SQLException e) 
	  {
		  System.err.println("Mysql Statement Error: " + query);
		  e.printStackTrace();
	  }	  
	  return "error";
  }
  
  
  public int getLastId()
  {
	  String query = "SELECT max(category_id) FROM category";
	  try
	  {
		  database db = new database();
		  Connection conn = db.getConn();
		  Statement select = conn.createStatement();
	      ResultSet result = select.executeQuery(query);
	      result.first();	      
	      return result.getInt(1);
	  }
	  catch(SQLException e) 
	  {
		  System.err.println("Mysql Statement Error: " + query);
		  e.printStackTrace();
	  }	  
	  return 0;
  }
  
}
  
  